import { Injectable } from '@nestjs/common';
import { InjectEntityManager, InjectRepository } from '@nestjs/typeorm';
import { EntityManager, Repository } from 'typeorm';
import { User } from '../../entities/user.entity';
import { City } from 'src/entities/city.entity';
import { Article } from 'src/entities/article.entity';

@Injectable()
export class TypeOrmsService {
  @InjectEntityManager()
  private manager: EntityManager;

  @InjectRepository(User)
  private userRepository: Repository<User>;

  async findTree() {
    const city = new City();
    city.name = '华南';
    await this.manager.save(city);

    const cityChild1 = new City()
    cityChild1.name = '云南'
    const parent = await this.manager.findOne(City, {
      where: {
        name: '华南'
      }
    });
    if(parent){
      cityChild1.parent = parent
    }
    await this.manager.save(City, cityChild1)

    const cityChild2 = new City()
    cityChild2.name = '昆明'

    const parent2 = await this.manager.findOne(City, {
      where: {
        name: '云南'
      }
    });
    if(parent){
      cityChild2.parent = parent2
    }
    await this.manager.save(City, cityChild2)

    console.log('查所有根节点 => ', this.manager.getTreeRepository(City).findRoots())

    console.log('查某个节点的所有后代节点 => ', this.manager.getTreeRepository(City).findDescendantsTree(parent2))
    console.log('查某个节点的所有后代节点-扁平化 => ', this.manager.getTreeRepository(City).findDescendants(parent2))

    console.log('查某个节点的祖先节点 => ', this.manager.getTreeRepository(City).findAncestorsTree(parent))
    console.log('查某个节点的祖先节点-扁平化 => ', this.manager.getTreeRepository(City).findAncestors(parent))

    console.log('查全部节点 => ', this.manager.getTreeRepository(City).findTrees())
    console.log('查全部节点-扁平化 => ', this.manager.getTreeRepository(City).find())

    console.log('查祖先节点个数 => ', this.manager.getTreeRepository(City).countAncestors(parent))
    console.log('查后代节点个数 => ', this.manager.getTreeRepository(City).countDescendants(parent))

    return this.manager.getTreeRepository(City).findTrees();
  }

  async initArticleData() {
      const a1 = new Article();
      a1.title = "夏日经济“热力”十足 “点燃”文旅消费新活力";
      a1.content = "人民网北京6月17日电 （高清扬）高考结束、暑期将至，各地文旅市场持续火热，暑期出游迎来热潮。热气腾腾的“夏日经济”成为消费活力升级的缩影，展示出我国文旅产业的持续发展势头。";

      const a2 = new Article();
      a2.title = "科学把握全面深化改革的方法要求";
      a2.content = "科学的方法是做好一切工作的重要保证。全面深化改革是一场复杂而深刻的社会变革，必须运用科学方法才能取得成功。";

      await this.manager.save(Article, a1);
      await this.manager.save(Article, a2);
  }

  create(createUserDto) {
    // this.manager.save(User, createUserDto);
    this.userRepository.save(createUserDto);
  }

  findAll() {
    return this.manager.find(User);
    // return this.userRepository.find(User)
  }

  findOne(id: number) {
    return this.manager.findOne(User, {
      where: { id },
    });
  }

  update(id: number, updateUserDto) {
    this.manager.save(User, {
      id: id,
      ...updateUserDto,
    });
  }

  remove(id: number) {
    this.manager.delete(User, id);
  }
}
